package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.BaseEnum;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.GameCurrencyDetailMapper;
import com.mysterymall.Mapper.UserMapper;
import com.mysterymall.Model.Entity.GameCurrencyDetail;
import com.mysterymall.Model.Entity.OrderInfo;
import com.mysterymall.Model.Entity.User;
import com.mysterymall.Service.GameCurrencyDetailService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/12/09  13:14
 */
@Service
public class GameCurrencyDetailServiceImpl implements GameCurrencyDetailService {

    private final GameCurrencyDetailMapper gameCurrencyDetailMapper;

    private final UserMapper userMapper;

    /**
     * 构造器注入
     * @param gameCurrencyDetailMapper 游戏币明细映射器
     */
    public GameCurrencyDetailServiceImpl(GameCurrencyDetailMapper gameCurrencyDetailMapper, UserMapper useMapper) {
        this.gameCurrencyDetailMapper = gameCurrencyDetailMapper;
        this.userMapper = useMapper;
    }

    @Override
    public Result getGameCurrencyDetail(HttpServletRequest request) {

        User user = userMapper.selectById(StpUtil.getLoginId().toString());

        LambdaQueryWrapper<GameCurrencyDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GameCurrencyDetail::getUserId, user.getId());

        // 按时间倒序
        wrapper.orderByDesc(GameCurrencyDetail::getCreateTime);

        List<GameCurrencyDetail>   list = gameCurrencyDetailMapper.selectList(wrapper);

        List<GameCurrencyDetail> list1 = new ArrayList<>();

        for (GameCurrencyDetail gameCurrencyDetail : list) {
            try {
                switch ((String) request.getAttribute("Lang")) {
                    case "zh":
                        gameCurrencyDetail.setGoodsName(gameCurrencyDetail.getGoodsName().split("_")[0]);
                        break;
                    case "ms":
                        gameCurrencyDetail.setGoodsName(gameCurrencyDetail.getGoodsName().split("_")[1]);
                        break;
                    case "en":
                        gameCurrencyDetail.setGoodsName(gameCurrencyDetail.getGoodsName().split("_")[2]);
                        break;
                }
            }catch (Exception e) {
                gameCurrencyDetail.setGoodsName(gameCurrencyDetail.getGoodsName());
            }
            list1.add(gameCurrencyDetail);
        }

        return new Result(HttpStatus.SUCCESS,"message.querySuccess",list1);
    }


}
